package com.colldown.iservice.dataShow.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.xml.ws.WebServiceRef;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.colldown.common.A_Tools;
import com.colldown.iservice.dataShow.dao.IDataShowDao;
import com.colldown.iservice.dataShow.service.IDataShowService;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
@Service
public class DataShowServiceImpl implements IDataShowService {
	@Autowired
	IDataShowDao dataShowDao;

	@Override
	public List queryAddrDateCharts(Map map) {
		return this.dataShowDao.queryAddrDateCharts(map);
	}
	
	/**
	 * 根据设备ids集查询设备备注信息
	 * @param paramMap
	 * @return
	 */
	public List queryDeviceListByIds(Map paramMap){
		return this.dataShowDao.queryDeviceListByIds(paramMap);
	}
	
	/**
	* @Title: dealCurveChartsData
	* @Description: TODO(拼接折线图数据)
	* @author Wayne
	* @return
	 */
	public JsonObject dealCurveChartsData(List<Map> list){
		//往前台返回的数据
		JsonObject rstJson = new JsonObject();
		//图表x轴 显示信息
		TreeSet<String> cateSet = new TreeSet();
		//根据地址分折线
		Set<String> addrSet = new HashSet();
		for(Map map :list){
			cateSet.add("'"+A_Tools.NullToStr(map.get("CURDAY"))+"'");
			addrSet.add(A_Tools.NullToStr(map.get("TERMINAL_ADDRESS")));
		}
		rstJson.addProperty("categories", cateSet.toString());
		
		Map deviceMap = new HashMap();
		if(addrSet.size() > 0){
			deviceMap.put("DEVICEIDS", addrSet);
		}else{
			Set<String> errSet = new HashSet();
			errSet.add("NODEVICE");
			deviceMap.put("DEVICEIDS", errSet);
		}
		List deviceList = this.queryDeviceListByIds(deviceMap);
		List deviceJsonList = new ArrayList();
		Gson gson = new Gson();
		//转化二进制为字符串
		for(Map m :(List<Map>)deviceList){
			m.put("TERMINAL_ADDRESS",new String((byte[])m.get("TERMINAL_ADDRESS")).trim());
			deviceJsonList.add(gson.toJson(m));
		}
		rstJson.addProperty("device", deviceJsonList.toString());
		
		//图表数据字段series 显示信息 
		List seriesList = new ArrayList();
		
		for(String str:addrSet){
			//series中单条线的数据信息
			Map seriesMap = new HashMap();
			//设置seriesMap的name字段
			seriesMap.put("name", str);
			
			//定义list存储data字段数据
			List joDatalist = new ArrayList();
			for(Map map :list){
				if(str.equals(map.get("TERMINAL_ADDRESS"))){
					joDatalist.add(map.get("TIME_DIFF"));
				}
			}
			//设置seriesMap的data字段
			seriesMap.put("data", joDatalist);
			//放入series列表
			seriesList.add(gson.toJson(seriesMap));
		}
		rstJson.addProperty("series", seriesList.toString());
		return rstJson;
	}
	
	/**
	* @Title: queryMaxMinAvgSec
	* @Description: TODO(查询最大、最小、平均延迟)
	* @param paramMap
	 */
	public List queryMaxMinAvgSec(Map paramMap){
		return this.dataShowDao.queryMaxMinAvgSec(paramMap);
	}
	/**
	* @Title: queryMaxMinAvgSec
	* @Description: TODO(查询在某一延迟区间内设备个数)
	* @param paramMap
	 */
	public int queryDeviceNumByDelaySec(Map paramMap){
		return this.dataShowDao.queryDeviceNumByDelaySec(paramMap);
	}

	/**
	* @Title: dealCurveChartsData
	* @Description: TODO(拼接柱状图数据)
	* @author Wayne
	* @return
	 */
	@Override
	public JsonObject dealBarChartsData(Map paramMap) {
		//获取x轴数据
		List<Map> xlist = this.queryMaxMinAvgSec(paramMap);
		Map xmap = xlist.get(0);
		int maxsec = A_Tools.NullToNumber(xmap.get("MAXSEC"));
		int minsec = A_Tools.NullToNumber(xmap.get("MINSEC"));
		int persec = Math.round(Float.parseFloat(Math.ceil((maxsec-minsec)/3)+""));
		//图表x轴 显示信息
		List<String> cateSet = new ArrayList();
		List xParamList = new ArrayList();
		//图标y轴数据信息
		List<Integer> ySet = new ArrayList();
		
		if(minsec!=0){
			//设置 0-最小值区间的查询参数
			Map firstMap = new HashMap();
			firstMap.put("BEGINSEC", 0);
			firstMap.put("ENDSEC", minsec);
			xParamList.add(firstMap);
			//0-最小值 区间对应的x轴下标显示
			cateSet.add("'0-"+minsec+"'");
		}

		for(int i=0;i<4;i++){
			Map xParamMap = new HashMap();
			xParamMap.put("BEGINSEC",minsec+persec*i );
			xParamMap.put("ENDSEC",minsec+persec*(i+1));
			xParamList.add(xParamMap);
			cateSet.add("'"+xParamMap.get("BEGINSEC")+"-"+xParamMap.get("ENDSEC")+"'");
		}
		
		for(Map m :(List<Map>)xParamList){
			paramMap.put("BEGINSEC",m.get("BEGINSEC"));
			paramMap.put("ENDSEC",m.get("ENDSEC"));
			ySet.add(this.queryDeviceNumByDelaySec(paramMap));
		}
		
		//往前台返回的数据
		JsonObject rstJson = new JsonObject();
		rstJson.addProperty("categories", cateSet.toString());
		rstJson.addProperty("series", ySet.toString());
		return rstJson;
	}

	/**
	* @Title: dealCurveChartsData
	* @Description: TODO(拼接饼图数据)
	* @author Wayne
	* @return
	 */
	@Override
	public JsonObject dealPieChartsData(Map paramMap) {
		//获取x轴数据
		List<Map> xlist = this.queryMaxMinAvgSec(paramMap);
		Map xmap = xlist.get(0);
		int maxsec = A_Tools.NullToNumber(xmap.get("MAXSEC"));
		int minsec = A_Tools.NullToNumber(xmap.get("MINSEC"));
		int persec = Math.round(Float.parseFloat(Math.ceil((maxsec-minsec)/3)+""));
		List xParamList = new ArrayList();
		//图表数据信息
		List<Map> ySet = new ArrayList();
		
		if(minsec!=0){
			//设置 0-最小值区间的查询参数
			Map firstMap = new HashMap();
			firstMap.put("BEGINSEC", 0);
			firstMap.put("ENDSEC", minsec);
			firstMap.put("PIENAME", "'0-"+minsec+"'"); //pie图显示名称
		}

		for(int i=0;i<4;i++){
			Map xParamMap = new HashMap();
			xParamMap.put("BEGINSEC",minsec+persec*i );
			xParamMap.put("ENDSEC",minsec+persec*(i+1));
			//pie图显示名称
			xParamMap.put("PIENAME", "'"+xParamMap.get("BEGINSEC")+"-"+xParamMap.get("ENDSEC")+"'");
			xParamList.add(xParamMap);
		}
		
		for(Map m :(List<Map>)xParamList){
			Map rstMap = new HashMap();
			paramMap.put("BEGINSEC",m.get("BEGINSEC"));
			paramMap.put("ENDSEC",m.get("ENDSEC"));
			rstMap.put("name", m.get("PIENAME"));
			rstMap.put("y", this.queryDeviceNumByDelaySec(paramMap));
			ySet.add(rstMap);
		}
		//往前台返回的数据
		JsonObject rstJson = new JsonObject();
		Gson gson = new Gson();
		rstJson.addProperty("series", gson.toJson(ySet));
		return rstJson;
	}

}
